#include "process_pool.h"
int child_handle(int);
int make_child(process_data_t* p,int processNum)
{
    int sv[2];
    //类似于无名管道，只用于同具有亲缘关系的进程间通信
    int ret,i;
    pid_t pid;
    for(i=0;i<processNum;++i)
    {
        //创建几个子进程，就要几个通信的管道
        ret=socketpair(AF_LOCAL,SOCK_STREAM,0,sv);
        ERROR_CHECK(ret,-1,"socketpair");
        pid=fork();
        if(!pid)
        {
            close(sv[0]);
            child_handle(sv[1]);
            break;
        }
        //子进程一直处在handle中的while(1)循环中
        //故只有父进程在进行for循环
        close(sv[1]);
        p[i].pid=pid;
        p[i].fd=sv[0];//calloc之后，busy默认为0
    }
    return 0;
}
int child_handle(int fds)
{
    int newfd;
    char doneFlag='d';
    while(1)
    {
        //拿到父进程分配的任务
        recvfd(fds,&newfd);
        printf("%d got assignment %d\n",getpid(),newfd);
        trans_file(newfd);
        close(newfd);
        printf("Assignment done\n");
        write(fds,&doneFlag,1);
    }
    exit(0);
}
